﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
using System.Globalization;

namespace MvcApplication1.Controllers
{
    public class DichVuHoTroController : Controller
    {
        //
        // GET: /DichVuHoTro/

        [HttpGet]
        public ActionResult DangKySuaChuaSanPham()
        {
            if (Session["tendangnhap"] == null || Session["tendangnhap"].ToString() == "")
            {
                return RedirectToAction("DangNhap", "TaiKhoan");
            }
            DataClassesDataContext dataContext = new DataClassesDataContext();
            var dsLoaiSanPham = from loaiDichVu in dataContext.MucSanPhams select loaiDichVu;
            ViewBag.DsLoaiSanPham = new SelectList(dsLoaiSanPham, "Id", "TenMucSp");
            ViewBag.MenuClass = Session["MenuClass"];
            return View();
        }

        [HttpPost]
        public ActionResult DangKySuaChuaSanPham(DonDangKyDichVuModel dichVuDangKyModel)
        {
            DataClassesDataContext dataContext = new DataClassesDataContext();
            DonDKDichVu donDangKy = new DonDKDichVu();
            donDangKy.DichVu_Id = 1;
            string tenDangNhap = Session["tendangnhap"].ToString();
            donDangKy.KhachHang_Id = (from user in dataContext.KhachHangs where user.UserName.Equals(tenDangNhap) select user.Id).FirstOrDefault();
            donDangKy.SanPhamId = dichVuDangKyModel.sanPhamId;
            donDangKy.NgayMuaSanPham = dichVuDangKyModel.ngayDangKy;
            donDangKy.MoTa = dichVuDangKyModel.moTaChiTiet;
            donDangKy.NgayDangKySuaChua = dichVuDangKyModel.ngayDangKySuaChua;
            donDangKy.TinhTrang = "Chưa sửa chữa";
            donDangKy.NgayGiaoHang = "";
            donDangKy.YKienPhanHoi = "";
            dataContext.DonDKDichVus.InsertOnSubmit(donDangKy);
            dataContext.SubmitChanges();
            ViewBag.MenuClass = Session["MenuClass"];
            return RedirectToAction("TheoDoiTinhTrangSuaChua");
        }

        [HttpPost]
        public JsonResult GetDsSanPham(int loaiSanPhamId)
        {
            DataClassesDataContext dataContext = new DataClassesDataContext();
            return this.Json(
                      new
                      {
                          Result = (from sanPham in dataContext.SanPhams where sanPham.MucSp_Id == loaiSanPhamId select new { Id = sanPham.Id, Name = sanPham.TenSp })
                      }
                      , JsonRequestBehavior.AllowGet
                   );
        }

        [HttpGet]
        public ActionResult TheoDoiTinhTrangSuaChua()
        {
            if (Session["tendangnhap"] == null || Session["tendangnhap"].ToString() == "")
            {
                return RedirectToAction("DangNhap", "TaiKhoan");
            }
            DataClassesDataContext dataContext = new DataClassesDataContext();
            string tenDangNhap = Session["tendangnhap"].ToString();
            int KhachHang_Id = (from user in dataContext.KhachHangs where user.UserName.Equals(tenDangNhap) select user.Id).FirstOrDefault();
            var dsDonDangKySuaChua = from donDangKySuaChua in dataContext.DonDKDichVus where donDangKySuaChua.KhachHang_Id == KhachHang_Id select donDangKySuaChua;
            List<DonDangKyDichVuModel> dsDangKy = new List<DonDangKyDichVuModel>();
            foreach (DonDKDichVu donDangKy in dsDonDangKySuaChua)
            {
                DonDangKyDichVuModel donDangKyModel = new DonDangKyDichVuModel();
                string tenSanPham = (from sanPham in dataContext.SanPhams where sanPham.Id == donDangKy.SanPhamId select sanPham).FirstOrDefault().TenSp;
                donDangKyModel.id = donDangKy.Id;
                donDangKyModel.tenSanPham = tenSanPham;
                donDangKyModel.ngayDangKySuaChua = donDangKy.NgayDangKySuaChua;
                donDangKyModel.tinhTrang = donDangKy.TinhTrang;
                donDangKyModel.ngayGiaoHang = donDangKy.NgayGiaoHang;
                donDangKyModel.moTaChiTiet = donDangKy.MoTa;
                donDangKyModel.yKienPhanHoi = donDangKy.YKienPhanHoi;
                dsDangKy.Add(donDangKyModel);
            }
            ViewBag.DanhSachDangKy = dsDangKy;
            if(dsDangKy.Count == 0) {
                ViewBag.EmptyState = "is-empty";
                ViewBag.DangKySuaChuaEmpty = "Không tìm thấy kết quả nào"; 
            }
            ViewBag.MenuClass = Session["MenuClass"];
            return View();
        }

        public ActionResult ThacMacVaGiaiDap()
        {
            DataClassesDataContext dataContext = new DataClassesDataContext();
            string tenDangNhap = Session["tendangnhap"].ToString();
            int KhachHang_Id = (from user in dataContext.KhachHangs where user.UserName.Equals(tenDangNhap) select user.Id).FirstOrDefault();
            var dsCauHoi = from cauHoi in dataContext.CauHoi_TraLois where cauHoi.KhachHang_Id == KhachHang_Id select cauHoi;
            List<CauHoi_TraLoiModel> dsCauHoiModels = new List<CauHoi_TraLoiModel>();
            foreach (CauHoi_TraLoi cauHoi in dsCauHoi)
            {
                CauHoi_TraLoiModel cauHoiModel = new CauHoi_TraLoiModel();
                cauHoiModel.cauHoi = cauHoi.CauHoi;
                cauHoiModel.traLoi = cauHoi.TraLoi;
                dsCauHoiModels.Add(cauHoiModel);
            }
            if (dsCauHoiModels.Count == 0)
            {
                ViewBag.EmptyState = "is-empty";
                ViewBag.CauHoiEmpty = "Không tìm thấy kết quả nào";
            }
            ViewBag.MenuClass = Session["MenuClass"];
            ViewBag.DanhSachCauHoiModel = dsCauHoiModels;
            return View();
        }

        [HttpPost]
        public ActionResult ThacMacVaGiaiDap(CauHoi_TraLoiModel cauHoiModel)
        {
            DataClassesDataContext dataContext = new DataClassesDataContext();
            CauHoi_TraLoi cauHoi = new CauHoi_TraLoi();
            string tenDangNhap = Session["tendangnhap"].ToString();
            int KhachHang_Id = (from user in dataContext.KhachHangs where user.UserName.Equals(tenDangNhap) select user.Id).FirstOrDefault();
            cauHoi.KhachHang_Id = KhachHang_Id;
            cauHoi.CauHoi = cauHoiModel.cauHoi;
            cauHoi.TraLoi = "";
            dataContext.CauHoi_TraLois.InsertOnSubmit(cauHoi);
            dataContext.SubmitChanges();
            return RedirectToAction("ThacMacVaGiaiDap");
        }

        [HttpPost]
        public JsonResult PhanHoi(int donhangid, string phanHoi)
        {
            DataClassesDataContext dataContext = new DataClassesDataContext();
            DonDKDichVu donDangKyDichVu = (from donDangKy in dataContext.DonDKDichVus where donDangKy.Id == donhangid select donDangKy).FirstOrDefault();
            donDangKyDichVu.YKienPhanHoi = phanHoi;
            dataContext.SubmitChanges();
            return this.Json(
                      new
                      {
                          id = donhangid,
                          text = phanHoi
                      }
                      , JsonRequestBehavior.AllowGet
                   );
        }
    }
}
